<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="客户名称" prop="customerName">
        <a-input v-model="form.customerName" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="客户手机" prop="phoneNumber">
        <a-input v-model="form.phoneNumber" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="邮箱" prop="email">
        <a-input v-model="form.email" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="公司" prop="company">
        <a-input v-model="form.company" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="部门" prop="department">
        <a-input v-model="form.department" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">保存</a-button>
          <a-button type="dashed" @click="cancel">取消</a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getCustomer, addCustomer, updateCustomer } from '@/api/erp/customer'

export default {
  name: 'CreateForm',
  data () {
    return {
      formTitle: '',
      open: false,
      // 表单参数
      form: {
        customerName: undefined,
        phoneNumber: undefined,
        email: undefined,
        company: undefined,
        department: undefined,
        remark: undefined,
        status: '0',
        customerId: undefined
      },
      rules: {
        customerName: [
          { required: true, message: '客户名不能为空', trigger: 'blur' }
        ],
        phoneNumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请正确填写手机号',
            trigger: 'blur'
          }
        ],
        email: [
          {
            type: 'email',
            message: '请正确填写邮箱地址',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.formTitle = '添加客户信息'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      const customerId = row ? row.customerId : ids
      getCustomer(customerId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改客户信息'
      })
    },
    // 表单重置
    reset () {
      this.form = {
        customerName: undefined,
        phoneNumber: undefined,
        email: undefined,
        company: undefined,
        department: undefined,
        remark: undefined,
        status: '0',
        customerId: undefined
      }
    },
    onClose () {
      this.open = false
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.customerId !== undefined) {
            updateCustomer(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          } else {
            addCustomer(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    }
  }
}
</script>
